/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package medicalstorebilling.ui.tab1order;

import database.dao.OrderDetailsDao;
import database.dao.OrdersDao;
import database.dao.StockDetailsDao;
import database.entity.Customers;
import database.entity.OrderDetails;
import database.entity.Orders;
import database.entity.StockDetails;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import medicalstorebilling.DatePicker;
import medicalstorebilling.ulti.DateConvert;
import medicalstorebilling.ui.tab2purchase.SelectorPopup;

/**
 *
 * @author Administrator
 */
public class Phase2Order extends javax.swing.JPanel {

    /**
     * Creates new form Phase2Order
     */
    private Phase1Order parent;
    private GroupLayout mainLayout;
    private JPanel previous;
    private Orders order;
    private Customers customer = new Customers();
    private List<OrderDetails> orderDetailsList = new ArrayList<OrderDetails>();
    private StockDetails stock = new StockDetails();
    private OrdersDao orderDao = new OrdersDao();
    private List<OrderDetails> deleteList = new ArrayList<OrderDetails>();
    private OrderDetailsDao orderDetailDao = new OrderDetailsDao();
    private StockDetailsDao stockDetailDao = new StockDetailsDao();

    public Customers getCustomer() {
        return customer;
    }

    public StockDetails getStock() {
        return stock;
    }

    public void setStock(StockDetails stock) {
        this.stock = stock;
        if (txtQuantity != null) {
            updateStockInfo();
        }
    }

    public void setCustomer(Customers customer) {
        Customers old = getCustomer();
        this.customer = customer;
        this.firePropertyChange("customer", old, customer);
        updatePrice();
    }

    public List<OrderDetails> getOrderDetailsList() {
        return orderDetailsList;
    }

    public void setOrderDetailsList(List<OrderDetails> orderDetailsList) {
        List<OrderDetails> old = getOrderDetailsList();
        this.orderDetailsList = orderDetailsList;
        this.firePropertyChange("orderDetailsList", old, orderDetailsList);
        updatePrice();
    }

    public Orders getOrders() {

        return order;
    }

    public void setOrders(Orders orders) {
        this.order = orders;
        this.stock = new StockDetails();
        this.deleteList = new ArrayList<OrderDetails>();
        updateIF();
    }

    public Phase2Order() {
        initComponents();
    }

    public Phase2Order(Phase1Order parent, GroupLayout mainLayout, JPanel previous) {
        this.parent = parent;
        this.mainLayout = mainLayout;
        this.previous = previous;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jLabel9 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        txtOrderName = new javax.swing.JTextField();
        txtCreatedDate = new javax.swing.JTextField();
        txtDeliveredDate = new javax.swing.JTextField();
        txtDistance = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        btnSelectCustomer = new javax.swing.JButton();
        txtState = new javax.swing.JTextField();
        btnEdit = new javax.swing.JButton();
        btnMakeFinal = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        txtQuantity = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtAvailable = new javax.swing.JLabel();
        txtMedName = new javax.swing.JLabel();
        txtMedCode = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnDeleteStock = new javax.swing.JButton();
        txtTotal = new javax.swing.JLabel();
        jButton9 = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(800, 580));

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel9.setText("<html><b>ORDER INFORMATION</b></html>");

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Order"));
        jPanel4.setPreferredSize(new java.awt.Dimension(790, 160));

        jLabel10.setText("Name");

        jLabel12.setText("Created Date");

        jLabel13.setText("State");

        txtCreatedDate.setEditable(false);
        txtCreatedDate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtCreatedDateMouseClicked(evt);
            }
        });

        txtDeliveredDate.setEditable(false);
        txtDeliveredDate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtDeliveredDateMouseClicked(evt);
            }
        });

        txtDistance.setToolTipText("<html>\nLess than 10km: Free.\n<br/>\nFrom 10 to 50km: Add 10% total.\n<br/>\nOver 50km: Add 15% total\n</html>");
        txtDistance.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtDistanceCaretUpdate(evt);
            }
        });

        jLabel11.setText("Distance (km)");

        jLabel14.setText("Delivered Date");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Customer"));

        jLabel1.setText("Name");

        jLabel2.setText("Address");

        jLabel3.setText("Phone");

        jLabel4.setText("Discount");

        jTextField1.setEditable(false);
        jTextField1.setMaximumSize(new java.awt.Dimension(0, 10));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.name}"), jTextField1, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jTextField2.setEditable(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.address}"), jTextField2, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jTextField3.setEditable(false);
        jTextField3.setToolTipText("<html>Level 0 means no discount.\n<br/>Level 1 means 10% total discount. \n<br/>Level 2 means 20% total discount</html>");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.discountLevel}"), jTextField3, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jTextField4.setEditable(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.phone}"), jTextField4, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        btnSelectCustomer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/search.png"))); // NOI18N
        btnSelectCustomer.setText("SEARCH");
        btnSelectCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectCustomerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSelectCustomer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField2)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSelectCustomer)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        txtState.setEditable(false);

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Edit.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnMakeFinal.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/stamp.png"))); // NOI18N
        btnMakeFinal.setText("Make Final");
        btnMakeFinal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMakeFinalActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addGap(10, 10, 10)
                        .addComponent(txtOrderName))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                                    .addComponent(jLabel12)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txtCreatedDate, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                                    .addComponent(jLabel14)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txtDeliveredDate)))
                            .addComponent(btnMakeFinal, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addGap(15, 15, 15)
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel13))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtState, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtDistance, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtOrderName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCreatedDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(jLabel11)
                    .addComponent(txtDistance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDeliveredDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14)
                    .addComponent(jLabel13)
                    .addComponent(txtState, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnMakeFinal)
                    .addComponent(btnEdit))
                .addGap(14, 27, Short.MAX_VALUE))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Select Medicine"));
        jPanel2.setPreferredSize(new java.awt.Dimension(790, 88));

        jLabel8.setText("<html><b>Quantity :</b></html>");

        jButton3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/search.png"))); // NOI18N
        jButton3.setText("SEARCH");
        jButton3.setPreferredSize(new java.awt.Dimension(160, 23));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Add.png"))); // NOI18N
        jButton4.setText("ADD");
        jButton4.setPreferredSize(new java.awt.Dimension(160, 23));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel5.setText("<html><b>Avaiable :</b></html>");

        txtAvailable.setText(" items !!!");

        txtMedName.setText("<html><b>Name :</b></html>");

        txtMedCode.setText("<html><b>Code : </b></html>");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtMedCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMedName, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtAvailable, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(95, 95, 95))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMedName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtAvailable))
                .addGap(8, 8, 8)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMedCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Order Details"));
        jPanel3.setPreferredSize(new java.awt.Dimension(790, 168));
        jPanel3.setVerifyInputWhenFocusTarget(false);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(760, 150));

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${orderDetailsList}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${stockDetail.medicine.code}"));
        columnBinding.setColumnName("Code");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${stockDetail.medicine.name}"));
        columnBinding.setColumnName("Name");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${stockDetail.expiredDate}"));
        columnBinding.setColumnName("Expired Date");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${stockDetail.medicine.price}"));
        columnBinding.setColumnName("Price per Unit");
        columnBinding.setColumnClass(java.math.BigDecimal.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${quantity}"));
        columnBinding.setColumnName("Quantity");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${total}"));
        columnBinding.setColumnName("Total");
        columnBinding.setColumnClass(Double.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setResizable(false);
        jTable1.getColumnModel().getColumn(3).setResizable(false);
        jTable1.getColumnModel().getColumn(4).setResizable(false);
        jTable1.getColumnModel().getColumn(5).setResizable(false);

        btnDeleteStock.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Delete.png"))); // NOI18N
        btnDeleteStock.setText("Delete Selected");
        btnDeleteStock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteStockActionPerformed(evt);
            }
        });

        txtTotal.setText("<html><b>Total: </b></html>");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnDeleteStock))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDeleteStock)
                    .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/back.png"))); // NOI18N
        jButton9.setText("Exit");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(175, 175, 175)
                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 10, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(53, Short.MAX_VALUE))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        this.parent.refresh(evt);
        this.deleteList = new ArrayList<OrderDetails>();
        this.mainLayout.replace(this, this.previous);
    }//GEN-LAST:event_jButton9ActionPerformed

    private void btnMakeFinalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMakeFinalActionPerformed
        if (JOptionPane.showConfirmDialog(null, "Finish this orders?\n You cannot edit anymore", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0) {
            if (btnEdit.getText().equals("Save")) {
                JOptionPane.showMessageDialog(null, "Please save current order first", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            order.setStatus(1);
            if (txtDeliveredDate.getText().trim().isEmpty()) {
                order.setDeliveredAt(new Date());
            } else {
                order.setDeliveredAt(DateConvert.convertDate(txtDeliveredDate.getText().trim()));
            }
            orderDao.update(order);
            JOptionPane.showMessageDialog(null, "Update Successfully", "Success", JOptionPane.INFORMATION_MESSAGE);
            updateIF();
        }
    }//GEN-LAST:event_btnMakeFinalActionPerformed

    private void btnSelectCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectCustomerActionPerformed
        // TODO add your handling code here:
        SelectorPopup selectDis = new SelectorPopup(null, true, customer, this);
        selectDis.setVisible(true);
    }//GEN-LAST:event_btnSelectCustomerActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        SelectorPopup selectDis = new SelectorPopup(null, true, stock, this);
        selectDis.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        if (this.orderDetailsList == null) {
            this.orderDetailsList = this.order.getOrder_details();
        }
        OrderDetails tmp = new OrderDetails();
        int quantity = 0;
        try {
            quantity = Integer.parseInt(txtQuantity.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Quantity must be an integer", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        setOrderDetailsList(new ArrayList<OrderDetails>());
        List<OrderDetails> list = new ArrayList<OrderDetails>();
        list.addAll(this.order.getOrder_details());
        for (Iterator<OrderDetails> it = list.iterator(); it.hasNext();) {
            OrderDetails orderDetails = it.next();
            if (orderDetails.getStockDetail().getId() == this.stock.getId()) {
                tmp = orderDetails;
                it.remove();
                break;
            }
        }
        if (tmp.getQuantity() + quantity > stock.getStockQuantity()) {
            JOptionPane.showMessageDialog(null, "Quantity cannot exceed " + stock.getStockQuantity(), "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            tmp.setQuantity(tmp.getQuantity() + quantity);
            tmp.setStockDetail(stock);
        }
        if (tmp.getId() != 0) {
            tmp.setChanged(true);
        }
        if (tmp.getQuantity() != 0) {
            list.add(tmp);
        }
        this.order.setOrder_details(list);
        this.setOrderDetailsList(this.order.getOrder_details());
    }//GEN-LAST:event_jButton4ActionPerformed

    private void btnDeleteStockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteStockActionPerformed
        // TODO add your handling code here:
        if (jTable1.getSelectedRowCount() <= 0) {
            JOptionPane.showMessageDialog(null, "Please select something to delete", "Error", JOptionPane.ERROR_MESSAGE);
        } else {

            List<OrderDetails> tmpList = new ArrayList<OrderDetails>();
            for (int i : jTable1.getSelectedRows()) {
                OrderDetails od = orderDetailsList.get(i);
                if (od.getId() != 0) {
                    deleteList.add(od);
                } else {
                    tmpList.add(od);
                }
            }
            if (!deleteList.isEmpty()) {
                this.order.getOrder_details().removeAll(deleteList);
            }
            if (!tmpList.isEmpty()) {
                this.order.getOrder_details().removeAll(tmpList);
            }
            setOrderDetailsList(null);
            setOrderDetailsList(this.order.getOrder_details());
        }
    }//GEN-LAST:event_btnDeleteStockActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        if (btnEdit.getText().equals("Edit")) {
            txtOrderName.setEditable(true);
            txtDistance.setEditable(true);
            btnDeleteStock.setVisible(true);
            setUIforEdit();
            btnMakeFinal.setEnabled(false);
        } else {

            String error = "";
            if (this.txtOrderName.getText().trim().isEmpty()) {
                error += "Order Name cannot be null\n";
            }
            if (this.orderDetailsList.isEmpty()) {
                error += "Order Details cannot be empty\n";
            }
            if (this.customer.getId() == 0) {
                error += "Customer cannot be empty\n";
            }
            Date createAt = DateConvert.convertDate(txtCreatedDate.getText());
            Date finishAt = DateConvert.convertDate(txtDeliveredDate.getText());
            if (createAt == null) {
                error += "Date create cannot be null\n";
            } else if (finishAt != null) {
                if (createAt.after(finishAt)) {
                    error += "Date Created must be before Date Delivered\n";
                }
            } else {
                if (createAt.after(new Date())) {
                    error += "Date Created can't be after today\n";
                }
            }
            if (!error.isEmpty()) {
                JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            this.order.setName(txtOrderName.getText().trim());
            this.order.setCreatedAt(createAt);
            if (finishAt == null) {
                this.order.setDeliveredAt(new Date());
            } else {
                this.order.setDeliveredAt(finishAt);
            }
            this.order.setCustomer(customer);
            this.order = orderDao.update(this.order);
            this.order.setRank(this.order.getCustomer().getDiscountLevel());
            for (OrderDetails orderDetails : orderDetailsList) {
                orderDetails.setOrder(this.order);
                orderDetailDao.update(orderDetails);
                if (orderDetails.isChanged()) {
                    StockDetails tmp = orderDetails.getStockDetail();
                    tmp.setStockQuantity(tmp.getStockQuantity() - orderDetails.getQuantity());
                    stockDetailDao.update(tmp);
                }
            }
            for (OrderDetails orderDetails : this.deleteList) {
                StockDetails tmp = orderDetails.getStockDetail();
                tmp.setStockQuantity(tmp.getStockQuantity() + orderDetails.getQuantity());
                stockDetailDao.update(tmp);
                orderDetailDao.delete(orderDetails);
            }
            setUIforView();
            btnDeleteStock.setVisible(false);
            btnMakeFinal.setEnabled(true);
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void txtDistanceCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtDistanceCaretUpdate
        // TODO add your handling code here:
        double distance = -1.0;
        try {
            distance = Double.parseDouble(txtDistance.getText().trim());
        } catch (NumberFormatException ex) {
            return;
        }
        if (distance >= 0) {
            this.order.setDistance(distance);
            updatePrice();
        }
    }//GEN-LAST:event_txtDistanceCaretUpdate

    private void txtCreatedDateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtCreatedDateMouseClicked
        if (btnEdit.getText().equals("Save")) {
            txtCreatedDate.setText(new DatePicker(null).setPickedDate());
        }
    }//GEN-LAST:event_txtCreatedDateMouseClicked

    private void txtDeliveredDateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtDeliveredDateMouseClicked
        if (btnEdit.getText().equals("Save")) {
            txtDeliveredDate.setText(new DatePicker(null).setPickedDate());
        }
    }//GEN-LAST:event_txtDeliveredDateMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDeleteStock;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnMakeFinal;
    private javax.swing.JButton btnSelectCustomer;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JLabel txtAvailable;
    private javax.swing.JTextField txtCreatedDate;
    private javax.swing.JTextField txtDeliveredDate;
    private javax.swing.JTextField txtDistance;
    private javax.swing.JLabel txtMedCode;
    private javax.swing.JLabel txtMedName;
    private javax.swing.JTextField txtOrderName;
    private javax.swing.JTextField txtQuantity;
    private javax.swing.JTextField txtState;
    private javax.swing.JLabel txtTotal;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private void updateIF() {
        btnEdit.setText("Edit");
        txtMedName.setText("<html><b>Medicine's Name :</b></html>");
        txtMedCode.setText("<html><b>Code : </b></html>");
        txtAvailable.setText("");
        if (order.getId() == 0) {
            this.stock = new StockDetails();
            setOrderDetailsList(new ArrayList<OrderDetails>());
            setCustomer(new Customers());
            btnEdit.setText("Save");
            btnEdit.setVisible(true);
            btnMakeFinal.setVisible(true);
            btnMakeFinal.setEnabled(false);
            jPanel2.setVisible(true);
            setNewOrder();
            btnDeleteStock.setVisible(true);
        } else if (order.getStatus() == 0) {
            btnEdit.setVisible(true);
            btnMakeFinal.setVisible(true);
            btnDeleteStock.setVisible(false);
        } else {
            btnEdit.setVisible(false);
            btnMakeFinal.setVisible(false);
            btnDeleteStock.setVisible(false);
        }
        if (order.getId() != 0) {
            
            txtOrderName.setEditable(false);
            txtDistance.setEditable(false);
            jPanel2.setVisible(false);
            btnSelectCustomer.setVisible(false);
            btnMakeFinal.setEnabled(true);
            setCustomer(order.getCustomer());
            setOrderDetailsList(order.getOrder_details());
            setOrderInformation();
        }
    }

    private void setOrderInformation() {
        txtOrderName.setText(order.getName());
        txtCreatedDate.setText(DateConvert.convertDateToString(order.getCreatedAt()));
        txtDeliveredDate.setText(DateConvert.convertDateToString(order.getDeliveredAt()));
        txtDistance.setText(String.valueOf(order.getDistance()));
        String status;
        switch (order.getStatus()) {
            case 0:
                status = "Pending";
                break;
            case 1:
                status = "Finished";
                break;
            default:
                status = "Unknown";
        }
        txtState.setText(status);
    }

    private void updateStockInfo() {
        txtMedName.setText("<html><b>Name : " + stock.getMedicine().getName() + "</b></html>");
        txtMedCode.setText("<html><b>Code : " + stock.getMedicine().getCode() + "</b></html>");
        txtAvailable.setText("<html><b>" + stock.getStockQuantity() + "items !!!</b></html>");
    }

    private void setNewOrder() {
        txtOrderName.setText("");
        txtCreatedDate.setText("");
        txtDeliveredDate.setText("");
        txtDistance.setText("");
        txtState.setText("Pending");
        btnSelectCustomer.setVisible(true);
        txtOrderName.setEditable(true);
        txtDistance.setEditable(true);

    }

    private void setUIforEdit() {
        btnEdit.setText("Save");
        jPanel2.setVisible(true);
        btnSelectCustomer.setVisible(true);
        this.stock = new StockDetails();
        txtMedName.setText("<html><b>Name :</b></html>");
        txtMedCode.setText("<html><b>Code : </b></html>");
        txtAvailable.setText("");
    }

    private void setUIforView() {
        btnEdit.setText("Edit");
        jPanel2.setVisible(false);
        btnSelectCustomer.setVisible(false);
        txtOrderName.setEditable(false);
        txtDistance.setEditable(false);
    }

    public void switchText(boolean flag) {
        txtOrderName.setEditable(flag);
        txtDistance.setEditable(flag);
    }

    private void updatePrice() {
        double total = 0.0;
        if (orderDetailsList == null) {
            txtTotal.setText("<html><b>Total : " + new DecimalFormat("#.##").format(total) + "</b></html>");
            return;
        }
        for (OrderDetails orderDetails : orderDetailsList) {
            total += orderDetails.getTotal();
        }
        double distance = this.order.getDistance();

        if (distance >= 10 && distance < 50) {
            total *= 1.1;
        } else if (distance >= 50) {
            total *= 1.15;
        }

        if (this.customer.getDiscountLevel() == 1) {
            total *= 0.9;
        } else if (this.customer.getDiscountLevel() == 2) {
            total *= 0.8;
        }
        txtTotal.setText("<html><b>Total : " + new DecimalFormat("'$' ###,###.##").format(total) + "</b></html>");
    }
}
